package com.doctor.utils.sys;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

import com.doctor.utils.logutils.LogUtils;


/**
 * Created by chemin
 * 可以在非UI线程弹的toast
 * on 2016/9/21.
 */
public class ToastUtils {
    /**
     * @param message toast 的信息 short的toast
     */
    public static void showShortToast(final Context context, final String message) {
        if (context != null) {
            if ("main".equals(Thread.currentThread().getName())) {
                Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
//                LogUtils.i("==========主线程的吐司"+Thread.currentThread().getName());
                return;
            }
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    LogUtils.i("==========吐司了多少次"+ Thread.currentThread().getName());
                }
            });

        }
    }

    /**
     * @param resources toast 资源string
     */
    public static void showShortToast(Context context, int resources) {
        if (context!=null){
            showShortToast(context,context.getString(resources));
        }
    }

    /**
     * @param message toast 的信息 long的toast
     */
    public static void showLongToast(final Context context, final String message) {
        if (context != null) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            });
        }
    }
    /**
     * resourId 资源ID
     */
    public static void showLongToast(Context context, int resourId) {
        if (context != null) {
            showLongToast(context,context.getString(resourId));
        }
    }


}
